package com.kangfawei.io;

import com.kangfawei.io.utils.FileUtils;

import java.io.*;

/**
 * @author 康发伟
 * 加入缓冲流进行文件拷贝
 */
public class BufferedTest {
    public static void main(String[] args) throws IOException {
        String src = "E:/IO_src/1.mp4";
        String target = "E:/IO_target/2.mp4";
        String target2 = "E:/IO_target/1.mp4";
        long start = System.currentTimeMillis();
        copy(src,target);
        System.out.println(System.currentTimeMillis() - start);

        FileUtils fileUtils = new FileUtils();
        InputStream is = new FileInputStream(src);
        OutputStream os = new FileOutputStream(target2);
        start = System.currentTimeMillis();
        fileUtils.copy(is,os);
        System.out.println(System.currentTimeMillis() - start);
    }

    public static void copy(String src,String target){


        try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream(src));
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(target));){


            byte[] flush = new byte[1024];
            int length = 0;

            while((length = bis.read(flush)) != -1){
                bos.write(flush,0,length);
            }
            bos.flush();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
